package cn.lngaosu.lwyd.module.integration.service.weixinusernum;

import cn.hutool.core.collection.CollUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.lngaosu.lwyd.module.integration.controller.admin.weixinusernum.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinusernum.WeixinUserNumDO;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;

import cn.lngaosu.lwyd.module.integration.dal.mysql.weixinusernum.WeixinUserNumMapper;

import static cn.lngaosu.lwyd.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lngaosu.lwyd.framework.common.util.collection.CollectionUtils.convertList;
import static cn.lngaosu.lwyd.framework.common.util.collection.CollectionUtils.diffList;
import static cn.lngaosu.lwyd.module.integration.enums.ErrorCodeConstants.*;

/**
 * 微信公众号用户数量 Service 实现类
 *
 * @author 张恒
 */
@Service
@Validated
public class WeixinUserNumServiceImpl implements WeixinUserNumService {

    @Resource
    private WeixinUserNumMapper weixinUserNumMapper;

    @Override
    public Long createWeixinUserNum(WeixinUserNumSaveReqVO createReqVO) {
        // 插入
        WeixinUserNumDO weixinUserNum = BeanUtils.toBean(createReqVO, WeixinUserNumDO.class);
        weixinUserNumMapper.insert(weixinUserNum);
        // 返回
        return weixinUserNum.getId();
    }

    @Override
    public void updateWeixinUserNum(WeixinUserNumSaveReqVO updateReqVO) {
        // 校验存在
        validateWeixinUserNumExists(updateReqVO.getId());
        // 更新
        WeixinUserNumDO updateObj = BeanUtils.toBean(updateReqVO, WeixinUserNumDO.class);
        weixinUserNumMapper.updateById(updateObj);
    }

    @Override
    public void deleteWeixinUserNum(Long id) {
        // 校验存在
        validateWeixinUserNumExists(id);
        // 删除
        weixinUserNumMapper.deleteById(id);
    }

    @Override
        public void deleteWeixinUserNumListByIds(List<Long> ids) {
        // 校验存在
        validateWeixinUserNumExists(ids);
        // 删除
        weixinUserNumMapper.deleteByIds(ids);
        }

    private void validateWeixinUserNumExists(List<Long> ids) {
        List<WeixinUserNumDO> list = weixinUserNumMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WEIXIN_USER_NUM_NOT_EXISTS);
        }
    }

    private void validateWeixinUserNumExists(Long id) {
        if (weixinUserNumMapper.selectById(id) == null) {
            throw exception(WEIXIN_USER_NUM_NOT_EXISTS);
        }
    }

    @Override
    public WeixinUserNumDO getWeixinUserNum(Long id) {
        return weixinUserNumMapper.selectById(id);
    }

    @Override
    public PageResult<WeixinUserNumDO> getWeixinUserNumPage(WeixinUserNumPageReqVO pageReqVO) {
        return weixinUserNumMapper.selectPage(pageReqVO);
    }

}